<script>
import { checkAutoLogin } from './utils/auth.js'
import themeManager from './utils/theme.js'

export default {
	onLaunch: function() {
		console.log('App Launch')
		
		// 初始化主题系统
		this.initTheme()
		
		// 监听主题变化事件
		this.setupThemeListener()
		
		// 检查自动登录
		checkAutoLogin()
	},
	onShow: function() {
		console.log('App Show')
	},
	onHide: function() {
		console.log('App Hide')
	},
	methods: {
		// 初始化主题系统
		initTheme() {
			try {
				// 加载保存的主题设置
				themeManager.loadTheme()
				
				console.log('主题系统初始化完成，当前主题：', themeManager.getCurrentTheme().name)
			} catch (error) {
				console.error('主题系统初始化失败：', error)
			}
		},
		
		// 设置主题监听器
		setupThemeListener() {
			// 监听主题变化事件
			uni.$on('theme-changed', (theme) => {
				console.log('主题已切换：', theme.name)
				// 强制重新渲染所有页面
				this.forcePageRefresh()
			})
		},
		
		// 强制页面刷新
		forcePageRefresh() {
			// 通过修改页面根元素的class来触发重新渲染
			const root = document.documentElement || document.body
			if (root) {
				root.classList.add('theme-refreshing')
				setTimeout(() => {
					root.classList.remove('theme-refreshing')
				}, 200)
			}
		}
	}
}
</script>

<style lang="scss">
	/* 引入全局样式变量 */
	@import './uni.scss';
	
	/* 全局松鼠主题样式 */
	page {
		background-color: $cute-bg-primary;
		font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Helvetica Neue', Helvetica, Arial, sans-serif;
		color: $cute-text-primary;
		line-height: 1.6;
	}
	
	/* 通用容器样式 */
	.container {
		background-color: $cute-bg-primary;
		min-height: 100vh;
		padding: 0;
	}
	
	/* 通用卡片样式 */
	.squirrel-card {
		background: $cute-bg-secondary;
		border-radius: $cute-border-radius;
		box-shadow: $cute-card-shadow;
		padding: 30rpx;
		margin: 20rpx;
		border: 1rpx solid $cute-border;
	}
	
	/* 通用按钮样式 */
	.squirrel-btn {
		background: linear-gradient(135deg, $cute-primary, $cute-primary-dark);
		color: $cute-text-inverse;
		border: none;
		border-radius: $cute-border-radius;
		padding: 24rpx 48rpx;
		font-size: 32rpx;
		font-weight: 600;
		box-shadow: $cute-button-shadow;
		transition: all 0.3s ease;
		
		&:active {
			transform: translateY(2rpx);
			box-shadow: 0 2rpx 8rpx $cute-shadow-medium;
		}
		
		&.secondary {
			background: linear-gradient(135deg, $cute-accent, $cute-accent-dark);
		}
		
		&.success {
			background: linear-gradient(135deg, $cute-success, #32CD32);
		}
		
		&.warning {
			background: linear-gradient(135deg, $cute-warning, #FFD700);
		}
		
		&.error {
			background: linear-gradient(135deg, $cute-error, #FF6B6B);
		}
	}
	
	/* 通用输入框样式 */
	.squirrel-input {
		background: $cute-bg-secondary;
		border: 2rpx solid $cute-border;
		border-radius: $cute-border-radius;
		padding: 24rpx;
		font-size: 32rpx;
		color: $cute-text-primary;
		box-shadow: $cute-input-shadow;
		transition: all 0.3s ease;
		
		&:focus {
			border-color: $cute-primary;
			box-shadow: 0 0 0 4rpx rgba(144, 238, 144, 0.1);
		}
		
		&::placeholder {
			color: $cute-text-tertiary;
		}
	}
	
	/* 通用标题样式 */
	.squirrel-title {
		font-size: 48rpx;
		font-weight: 700;
		color: $cute-text-primary;
		margin-bottom: 20rpx;
		text-align: center;
	}
	
	.squirrel-subtitle {
		font-size: 32rpx;
		font-weight: 500;
		color: $cute-text-secondary;
		margin-bottom: 16rpx;
		text-align: center;
	}
	
	/* 通用分割线样式 */
	.squirrel-divider {
		height: 2rpx;
		background: linear-gradient(90deg, transparent, $cute-divider, transparent);
		margin: 30rpx 0;
	}
	
	/* 通用图标样式 */
	.squirrel-icon {
		width: 80rpx;
		height: 80rpx;
		background: linear-gradient(135deg, $cute-accent, $cute-accent-light);
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		color: $cute-text-inverse;
		font-size: 36rpx;
		box-shadow: $cute-button-shadow;
	}
	
	/* 通用标签样式 */
	.squirrel-tag {
		background: $cute-accent-light;
		color: $cute-text-inverse;
		padding: 8rpx 16rpx;
		border-radius: 20rpx;
		font-size: 24rpx;
		font-weight: 500;
	}
	
	/* 响应式布局 */
	@media (max-width: 750rpx) {
		.squirrel-card {
			margin: 15rpx;
			padding: 25rpx;
		}
		
		.squirrel-title {
			font-size: 40rpx;
		}
		
		.squirrel-subtitle {
			font-size: 28rpx;
		}
	}
	
	/* 动画效果 */
	@keyframes squirrel-fade-in {
		from {
			opacity: 0;
			transform: translateY(20rpx);
		}
		to {
			opacity: 1;
			transform: translateY(0);
		}
	}
	
	@keyframes squirrel-bounce {
		0%, 20%, 53%, 80%, 100% {
			transform: translate3d(0, 0, 0);
		}
		40%, 43% {
			transform: translate3d(0, -8rpx, 0);
		}
		70% {
			transform: translate3d(0, -4rpx, 0);
		}
		90% {
			transform: translate3d(0, -2rpx, 0);
		}
	}
	
	.squirrel-fade-in {
		animation: squirrel-fade-in 0.6s ease-out;
	}
	
	.squirrel-bounce {
		animation: squirrel-bounce 1s ease-in-out;
	}
	
	/* 主题切换动画 */
	.theme-updating,
	.theme-refreshing {
		transition: all 0.3s ease;
	}
	
	/* 确保主题变量在所有元素上生效 */
	* {
		transition: background-color 0.3s ease, color 0.3s ease, border-color 0.3s ease;
	}
</style>
